<?php
$sql = "SELECT id,name,link FROM #__menu WHERE published=1 AND menutype='topmenu' AND parent=0 ORDER BY ordering ASC, id DESC";
$database->setQuery($sql);
$lsMenuTop = $database->loadObjectList(); 
?>
<script type="text/javascript">
$(document).ready(function () {	
	$('#nav li.xx').addClass('active');
	var meleft=$('#nav li.xx').position().left;
	var mewidth=$('#nav li.xx').width();
	var itemwidth=$('#nav li.xx ul').width();
	var a=mewidth/2;
	var b=itemwidth/2;
	if((meleft+a) > b){
		$('#nav li.xx ul').css({'left' : meleft+a-b +'px'});
	}
	$('#nav li.xx ul').show();
			
			
	$('#nav li', this).hover(
		function () {
			
			$('#nav li').removeClass('active');
			$('#nav li.xx ul').hide();
			$(this).addClass('active');
			
			var meleft=$(this).position().left;
			var mewidth=$(this).width();
			var itemwidth=$('ul', this).width();
			var a=mewidth/2;
			var b=itemwidth/2;
			if((meleft+a) > b){
				$('ul', this).css({'left' : meleft+a-b +'px'});
			}
			
			$('ul', this).show();
			
					
		}, 
		function () {
			$(this).removeClass('active');
			$('ul', this).hide();
			$('#nav li.xx').addClass('active');
			$('#nav li.xx ul').show();
		}
	);
});
</script>
<ul id="nav">
	<?php
	for ($i=0; $i<count($lsMenuTop); $i++){
		if ($i==0){
			$active = ' class="xx active"';
		}else{
			$active = ' class=""';
		}
		$sql = "SELECT name,link FROM #__menu WHERE published=1 AND menutype='topmenu' AND parent=".$lsMenuTop[$i]->id." ORDER BY ordering ASC, id DESC";
		$database->setQuery($sql);
		$lsMenuTopSub = $database->loadObjectList(); 	
		if (count($lsMenuTopSub)>0){
		?>
		<li id="item"<?php echo $active;?>><a href="<?php echo sefRelToAbs($lsMenuTop[$i]->link);?>" id="itemain"><?php echo $lsMenuTop[$i]->name;?></a>
			<ul>
				<?php 
				for ($j=0; $j<count($lsMenuTopSub); $j++){
					?>
					<li><a href="<?php echo sefRelToAbs($lsMenuTopSub[$j]->link);?>"><?php echo $lsMenuTopSub[$j]->name;?></a></li>
					<?php
				}	
				?>
			</ul>
		</li>
		<?php
		}else{
		?>
		<li id="item"<?php echo $active;?>><a href="<?php echo sefRelToAbs($lsMenuTop[$i]->link);?>" id="itemain"><?php echo $lsMenuTop[$i]->name;?></a></li>
		<?php
		}		 
		?>
		<li class="line"></li>
		<?php 
	}
	?>
</ul>